<template>
  <div class="pageBox">
    <head-component title="个人设置"></head-component>
    <div class="top flex-center">
      <div class="avatar" @click="show = true">
        <img :src="avatar" alt="" />
      </div>
      <div class="nickname flex-row" @click="$router.push('/info')">
        <p>{{ info.nickname }}</p>
        <img src="@/image/my/bainji_btn.png" alt="" />
      </div>
    </div>

    <div class="list">
      <div class="listItem flex-between" @click="goPage('/changePass')">
        <div class="left">登录密码</div>
        <div class="right flex-row">
          <p>去修改</p>
          <van-icon name="arrow" color="#C2C6CC" size="16" />
        </div>
      </div>
    </div>

    <van-action-sheet
      v-model:show="show"
      cancel-text="取消"
      @cancel="show = false"
    >
      <div class="sheet-item camera">
        拍照
        <input
          class="inputstyle"
          type="file"
          accept="image/*"
          capture="camera"
          @change="add_img($event)"
        />
      </div>
      <div class="sheet-item album">
        从相册中选择
        <input
          class="inputstyle"
          type="file"
          accept="image/*"
          @change="add_img($event)"
        />
      </div>
    </van-action-sheet>
  </div>
</template>

<script type="text/ecmascript-6">
import { defineComponent, ref } from "vue";
import headComponent from "../../components/headComponent.vue";
import router from "../../router";
import { post, upload } from "../../untils/request";

export default defineComponent({
  components: {
    headComponent,
  },
  setup() {
    const avatar = ref("");
    const show = ref(false);

    const info = ref({});

    function add_img(event) {
      var file = event.target.files[0];
      let formData = new FormData();
      formData.append("file", file);

      upload("/upload",formData).then((res) => {
       console.log("上传",res)
       avatar.value = res.data.file;
       show.value = false;
       changeInfo();
      });
    }

    function goPage(url) {
      router.push(url);
    }

    function getInfo() {
      post("/portal", {
        module: "User",
        interface: "1000",
      }).then((res) => {
        info.value = res.data;
        avatar.value = res.data.avatar;
        console.log(info.value);
      });
    }
    getInfo();

    function changeInfo() {
      post("/portal", {
        module: "User",
        interface: "1001",
        data: {
          avatar: avatar.value,
        }
      }).then(() => {
        getInfo();
      });
    }

    return {
      avatar,
      show,
      info,
      add_img,
      goPage,
      getInfo,
      changeInfo,
    };
  },
});
</script>

<style scoped lang="less">
.pageBox {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-top: 88px;
  .top {
    height: 282px;
    background-color: #fff;
    flex-direction: column;
    .avatar {
      width: 140px;
      height: 140px;
      border-radius: 50%;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .nickname {
      margin-top: 20px;
      p {
        font-size: 30px;
        font-weight: 500;
        color: #333333;
        margin-right: 10px;
      }
      img {
        width: 30px;
        height: 30px;
      }
    }
  }
  .sheet-item {
    height: 92px;
    line-height: 92px;
    font-size: 32px;
    text-align: center;

    .inputstyle {
      width: 100%;
      height: 92px;
      cursor: pointer;
      outline: medium none;
      position: absolute;
      filter: alpha(opacity=0);
      -moz-opacity: 0;
      opacity: 0;
      left: 0px;
      top: 0px;
    }
  }
  .camera {
    color: #ff3b30;
    position: relative;
  }
  .album {
    color: #007aff;
    position: relative;
  }
  .list {
    margin-top: 20px;
    background-color: #fff;
    padding: 0 30px;

    .listItem {
      width: 100%;
      height: 121px;
      .left {
        font-size: 28px;
        font-weight: 400;
        color: #333333;
      }
      .right {
        p {
          font-size: 28px;
          font-weight: 400;
          color: #c2c6cc;
        }
      }
    }
  }
}
</style>